package com.rchat.pocmini.activity.invitefriendtogroup;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.fragment.groups.GroupsPresenterImpl;
import com.rchat.pocmini.activity.view.HorizontalListView;
import com.rchat.pocmini.activity.view.chat.CustomTitleBar;
import com.rchat.pocmini.bean.UserNoInGroupBean;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/9/1.
 */

public class InviteFriendToGroupMvpActivity extends BaseMvpActivity<InviteFriendToGroupPresenterImpl,InviteFriendToGroupModelImpl> implements InviteFriendToGroupContract.View,TextView.OnEditorActionListener{


    @BindView(R.id.rl_showmenbers)
    RelativeLayout mRlShowSelectedMembers;

    @BindView(R.id.lv_allmenber)
    ListView lvAllMember;

    @BindView(R.id.ed_search)
    EditText mEtSearch;

    @BindView(R.id.ll_invite)
    LinearLayout mLlInvite;

    @BindView(R.id.ll_cancel)
    LinearLayout mLlCancel;

    @BindView(R.id.tv_toltal)
    TextView mTvSelectedCount;

    @BindView(R.id.customtitlebar)
    CustomTitleBar customtitlebar;

    @BindView(R.id.lv_horizon)
    HorizontalListView lvSelectedMember;

    // 搜索的词
    String mName;

    private InviteFriendNoInGroupAdapter adapter;
    private ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data;

    private SelectedMemberAdapter selectedMemberAdapter;
    private ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> selectedData;


    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);

        data = new ArrayList<>();
        adapter = new InviteFriendNoInGroupAdapter(this,data);

        selectedData = new ArrayList<>();
        selectedMemberAdapter = new SelectedMemberAdapter(this,selectedData);
    }

    @Override
    public int getLayoutId() {
        return R.layout.invite_friend_to_group;
    }

    @Override
    public void initView() {
        customtitlebar.setActivity(this);
        customtitlebar.setCenterTitle(R.string.invite_friend);
        lvAllMember.setAdapter(adapter);
        lvSelectedMember.setAdapter(selectedMemberAdapter);
        mEtSearch.setOnEditorActionListener(this);

        mPresenter.queryFriendNotInGroup(AirtalkeeAccount.getInstance().getUserId(),
                getIntent().getStringExtra(GroupsPresenterImpl.KEY_GROUPID));
    }

    @Override
    public void doAfterInitView() {

    }

    @Override
    public void queryFriendNotInGroup(ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data) {
        if(data!= null){
            this.data.clear();
            this.data.addAll(data);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showSelectedCount(int visible, String content) {
        if(visible == View.VISIBLE){
            mTvSelectedCount.setVisibility(visible);
            mTvSelectedCount.setText(content);
        }else {
            mTvSelectedCount.setVisibility(visible);
            mTvSelectedCount.setText("");
        }
    }

    @Override
    public void showSelectedListView(int visible, ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data) {
        mRlShowSelectedMembers.setVisibility(visible);
        if(data!=null){
            selectedData.clear();
            selectedData.addAll(data);
            selectedMemberAdapter.notifyDataSetChanged();
        }else {
            selectedData.clear();
            selectedMemberAdapter.notifyDataSetChanged();
        }

    }

    @OnClick({R.id.ll_invite,
            R.id.ll_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_invite:
                mPresenter.inviteFriendToGroup(getIntent().getStringExtra(GroupsPresenterImpl.KEY_GROUPID));
                break;
            case R.id.ll_cancel:
                mPresenter.clearSelected();
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            mName = mEtSearch.getText().toString().trim();

            mPresenter.searchFriend(mName);
        }
        return false;
    }
}
